package com.gz.moreaseManager.modules.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gz.moreaseManager.config.SelfUserDetails;
import com.gz.moreaseManager.core.modal.AjaxResult;
import com.gz.moreaseManager.core.modal.HttpCode;
import com.gz.moreaseManager.core.modal.PageResult;
import com.gz.moreaseManager.modules.entity.Product;
import com.gz.moreaseManager.modules.service.IProductService;
import com.gz.moreaseManager.modules.transfer.ProductSimpVo;
import com.gz.moreaseManager.modules.transfer.ProductTagListVo;
import com.gz.moreaseManager.modules.transfer.ProductVo;
import com.gz.moreaseManager.utils.DateUtil;
import com.gz.moreaseManager.utils.UtilTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 产品信息 前端控制器
 * </p>
 *
 * @author wike
 * @since 2019-02-27
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @GetMapping("list")
    public ModelAndView list(){
        return new ModelAndView("modules/product/list");
    }

    /**
     * 分页查询产品信息
     * @param name
     * @param type 产品标签
     * @param startDate
     * @param endDate
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("listPage")
    public PageResult<Product> findPageList(String name, Integer type,
                                            @DateTimeFormat(pattern = "yyyy-MM-dd")Date startDate,
                                            @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
                                            Integer pageNum, Integer pageSize){

        //分页
        Page paramsPage = new Page();
        paramsPage.setCurrent(pageNum);
        paramsPage.setSize(pageSize);
        //构建查询
        Wrapper wrapper = new EntityWrapper<Product>();
        if( UtilTool.isNotBlank( name ))
            wrapper.like("product_name",name);
        if( UtilTool.isNotBlank( type )){
            wrapper.where("FIND_IN_SET({0},tags)",type);
        }
        if( UtilTool.isNotBlank( startDate ) ){
            if( UtilTool.isNotBlank( endDate ) && startDate.getTime() == endDate.getTime() ){
                wrapper.between("release_time",startDate,DateUtil.addDays(startDate,1));
            }else{
                wrapper.between("release_time",startDate,
                        UtilTool.isNotBlank(endDate)?DateUtil.addDays(endDate,1):DateUtil.addDays(new Date(),1));
            }
        }
        if( UtilTool.isBlank(startDate) && UtilTool.isNotBlank( endDate )){
            wrapper.le("release_time",DateUtil.addDays(endDate,1));
        }
        //排序
        wrapper.orderBy("release_time",false);
        Page<Product> data = productService.selectPage(paramsPage,wrapper);
        return new PageResult(0, null, data.getTotal(), data.getRecords());

    }

    /**
     * 新增产品信息
     * @param productVo
     * @return
     */
    @PostMapping("addProduct")
    public AjaxResult<Boolean> addProduct(@RequestBody ProductVo productVo,Authentication authentication){
        if( productVo == null
                || UtilTool.isBlank( productVo.getProductName())
                || UtilTool.isBlank( productVo.getProductMessage())
                || UtilTool.isBlank( productVo.getPhotoFiles())
                || UtilTool.isBlank( productVo.getImgList())
                || UtilTool.isBlank( productVo.getSizeList())
                || UtilTool.isBlank( productVo.getSort())
                || UtilTool.isBlank( productVo.getStatus())
                || UtilTool.isBlank( productVo.getReleaseTime()))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");

        //创建人
        SelfUserDetails userDetails = (SelfUserDetails) authentication.getPrincipal();
        productVo.setCreatePerson( userDetails.getUsername());
        Boolean success = productService.addProduct( productVo );
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "新增产品信息失败!");
    }

    /**
     * 修改产品信息
     * @param productVo
     * @return
     */
    @PostMapping("editProduct")
    public AjaxResult<Boolean> editProduct(@RequestBody ProductVo productVo){
        if( productVo == null
                || UtilTool.isBlank( productVo.getProductId())
                || UtilTool.isBlank( productVo.getProductName())
                || UtilTool.isBlank( productVo.getProductMessage())
                || UtilTool.isBlank( productVo.getPhotoFiles())
                || UtilTool.isBlank( productVo.getImgList())
                || UtilTool.isBlank( productVo.getSizeList())
                || UtilTool.isBlank( productVo.getSort())
                || UtilTool.isBlank( productVo.getStatus())
                || UtilTool.isBlank( productVo.getReleaseTime()))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");

        Boolean success = productService.editProduct( productVo );
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "修改产品信息失败!");
    }

    /**
     * 根据产品ID删除产品
     * @param productId
     * @return
     */
    @PostMapping("delete")
    public AjaxResult<Boolean> delete( Integer productId ){

        if( UtilTool.isBlank( productId ))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");
        Boolean success = productService.deleteProduct( productId );
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "删除产品信息失败!");
    }

    /**
     * 根据产品id获取产品信息
     * @param productId
     * @return
     */
    @GetMapping("findProduct")
    public AjaxResult<ProductVo> findProduct( Integer productId ){

        if( UtilTool.isBlank( productId ))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");
        ProductVo productVo = productService.findProductById( productId );
        return  AjaxResult.success(productVo);
    }

    /**
     * 根据产品标签类型获取产品列表
     * @param tagId
     * @return
     */
    @GetMapping("findTagProductList")
    public AjaxResult<ProductTagListVo> findTagProductList(Integer tagId){
        if( UtilTool.isBlank( tagId ))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");
        ProductTagListVo tagListVo = productService.findTagProductList(tagId);
        return  AjaxResult.success(tagListVo);
    }

    /**
     * 获取置顶产品列表
     * @return
     */
    @GetMapping("findRecommendProduct")
    public AjaxResult<List<ProductSimpVo>> findProductListRecommend(){
        List<ProductSimpVo> list = productService.findRecommendProductList();
        if( UtilTool.isNotBlank( list ))
            return AjaxResult.success(list );
        return  AjaxResult.build(HttpCode.FAILED, "查询产品信息失败!");
    }

}
